// ==UserScript==
// @name           Google Translate Context
// @author         Roman Shafigullin
// @namespace      http://misbug.blogspot.com/
// @include        *
// ==/UserScript==

// http://code.google.com/apis/ajaxlanguage/documentation/
// http://ajax.googleapis.com/ajax/services/language/translate?v=1.0&q=test&langpair=en%7Cru

// http://translate.google.com/
// http://translate.google.com/translate_a/t?client=t&text=test&sl=en&tl=ru

(function()
{

var foreignLanguage = 'en';
var nativeLanguage = 'ru';

var wordPatterns = new Array();
wordPatterns['auto'] = /^\S+$/;
wordPatterns['en'] = /^[a-z]+$/i;

var ShowTip = function()
{
	
}

var GetWordFromEvent = function(event)
{
	if (document.body && document.body.createTextRange)
	{
		var range = document.body.createTextRange();
		range.moveToPoint(event.clientX, event.clientY);
		range.expand('word');
		return(range.text);
	}
	else if (event.rangeParent && document.createRange)
	{
		var range = document.createRange();
		var word = '';
		
		range.setStart(event.rangeParent, event.rangeOffset);
		range.setEnd(event.rangeParent, event.rangeOffset);
		
		try
		{
			range.setStart(range.startContainer, range.startOffset - 1);
		}
		catch(e)
		{
		}
		
		if (wordPatterns[foreignLanguage].test(range.toString()))
		{
			try
			{
				do
				{
					range.setStart(range.startContainer, range.startOffset - 1);
				}
				while(wordPatterns[foreignLanguage].test(range.toString()));
				range.setStart(range.startContainer, range.startOffset + 1);
			}
			catch(e)
			{
			}
			
			try
			{
				do
				{
					range.setEnd(range.endContainer, range.endOffset + 1);
				}
				while(wordPatterns[foreignLanguage].test(range.toString()));
				range.setEnd(range.endContainer, range.endOffset - 1);
			}
			catch(e)
			{
			}
			
			word = range.toString();
		
		}
		
		range.detach();
		
		return(word);
	}
	else
	{
		return('');
	}
}

var Translate = function(target, text, word)
{
	Translate.text = text;
	Translate.word = word;
	
	if (word.length < 30 && !(/^\s*$/).test(word))
	{
		if (typeof(Translate.translated[word]) == 'undefined')
		{
			Translate.translated[word] = true;
			window.status = 'translating...' + ' (' + word + ')';
			GM_xmlhttpRequest(
			{
				method: 'GET',
				url: 'http://translate.google.com/translate_a/t?client=t&text=' + encodeURIComponent(word) + '&sl=' + foreignLanguage + '&tl=' + nativeLanguage,
				onload: function(response)
				{
					if (response.status == 200)
					{
						Translate.translated[word] = eval(response.responseText);
						
						if (typeof(Translate.translated[word]) != 'undefined')
						{
							if (Translate.word == word)
							{
								window.status = Translate.translated[word] + ' (' + word + ')';
							}
						}
					}
				},
				onerror: function()
				{
					delete(Translate.translated[word]);
				}
			});
		}
	}
	
	if (text != word && text.length < 5000 && !(/^\s*$/).test(text))
	{
		if (typeof(Translate.translated[text]) == 'undefined')
		{
			Translate.translated[text] = true;
			GM_xmlhttpRequest(
			{
				method: 'GET',
				url: 'http://translate.google.com/translate_a/t?client=t&text=' + encodeURIComponent(text) + '&sl=' + foreignLanguage + '&tl=' + nativeLanguage,
				onload: function(response)
				{
					if (response.status == 200)
					{
						Translate.translated[text] = eval(response.responseText);
						
						if (typeof(Translate.translated[text]) != 'undefined')
						{
							target.setAttribute('title', Translate.translated[text]);
						}
					}
				},
				onerror: function()
				{
					delete(Translate.translated[text]);
				}
			});
		}
	}
}

Translate.translated = {};

window.addEventListener('mousemove', function(event)
{
	var word = GetWordFromEvent(event);
	
	var target = event.target;
	var text = target.textContent;
	
	clearTimeout(Translate.timerID);
	Translate.timerID = setTimeout(function(){Translate(target, text, word)}, 1000);
	
	if (typeof(Translate.translated[word]) == 'string')
	{
		window.status = Translate.translated[word] + ' (' + word + ')';
	}
	
}, false);


})();